﻿using Dapper;
using Infrastructure.DataBaseConnect;
using Infrastructure.IRepository.Sys;
using Model.BusinessModel;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using System.Text;

namespace Infrastructure.Repository.Sys
{
    /// <summary>
    /// 按钮样式仓储
    /// </summary>
    public class SysButtonStyleRepository : Repository<SysButtonStyle>, ISysButtonStyleRepository
    {
        /// <summary>
        /// 根据按钮样式名称，查询数据
        /// </summary>
        /// <param name="buttonStyleName">按钮样式名称</param>
        /// <returns></returns>
        public SysButtonStyle? GetSysButtonStyleByName(string buttonStyleName)
        {
            string sql = @" SELECT  * FROM Sys_ButtonStyle where ButtonStyleName=@ButtonStyleName";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.QueryFirstOrDefault<SysButtonStyle>(sql, new { ButtonStyleName = buttonStyleName });
        }

        /// <summary>
        /// 获取按钮样式数据
        /// </summary>
        /// <param name="authWhere">授权条件</param>
        /// <returns>返回按钮样式数据</returns>
        public List<SysButtonStyleOutPut> GetButtonStyleList(string authWhere)
        {
            StringBuilder sql = new(@" select bs.ButtonStyleId,bs.ButtonStyleName,bs.Icon,bs.Color,bs.TextColor,bs.Types,bs.Size,bs.IsText,bs.IsRound,bs.IsCircle,bs.IsPlain,bs.IsLink,bs.IsDisabled,bs.CreateTime,bs.CreateUser,su.UserName,count(sb.ButtonStyleId) UseCount from Sys_ButtonStyle as bs
                                        inner join Sys_User as su on bs.CreateUser = su.UserId 
                                         left join Sys_Button as sb on sb.ButtonStyleId = bs.ButtonStyleId
										 ");

            var qp = new QueryParameter();
            if (!string.IsNullOrWhiteSpace(authWhere))
            {
                qp.listWhere.Append(authWhere);
            }
            if (!string.IsNullOrEmpty(qp.listWhere.ToString()))
            {
                sql.Append(" WHERE 1=1 " + qp.listWhere.ToString());
            }
            sql.Append(" group by bs.ButtonStyleId,bs.ButtonStyleName,bs.Icon,bs.Color,bs.TextColor,bs.Types,bs.Size,bs.IsText,bs.IsRound,bs.IsCircle,bs.IsPlain,bs.IsLink,bs.IsDisabled,bs.CreateTime,bs.CreateUser,su.UserName  order by bs.CreateTime, UseCount desc ");
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysButtonStyleOutPut>(sql.ToString(), qp.dynamicParameter).ToList();
        }

        /// <summary>
        /// 获取按钮样式使用次数
        /// </summary>
        /// <param name="buttonStyleId"></param>
        /// <returns></returns>
        public int GetButtonStyleCount(string buttonStyleId)
        {
            string sql = @" select COUNT(1) as UseCount from  Sys_Button where ButtonStyleId = @ButtonStyleId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.QuerySingle<int>(sql, new { ButtonStyleId = buttonStyleId });
        }
    }
}
